using MicroMsg.Common.Utils;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
namespace MicroMsg.Network
{
	public class ShortConnector
	{
		private const int ERROR_COUNT_MAX = 3;
		private static int mFailedCount;
		public static bool isValid()
		{
			return true;
		}
		public static void onPrepare()
		{
		}
		public static bool isAvailable()
		{
			return ShortConnector.mFailedCount == 0;
		}
		public static bool checkReady()
		{
			return true;
		}
		public static void close()
		{
			SessionPackMgr.closeAllHttpClientInPacks();
		}
		public static void onSendFailed(bool needReconnect)
		{
			ShortConnector.mFailedCount++;
			if (ShortConnector.mFailedCount > 3)
			{
				LongConnector.setAvailable(true);
			}
			ShortConnector.close();
			HostService.mShortConnHosts.onCurrentHostFailed();
		}
		public static bool sendPack(SessionPack sessionPack)
		{
			HostService.HostInfo availableHost = HostService.mShortConnHosts.getAvailableHost();
			string cmdUri = string.Concat(new object[]
			{
				"http://",
				availableHost.mHost,
				":",
				availableHost.mPort,
				sessionPack.mCmdUri
			});
			string mHost = availableHost.mHost;
			HttpClient httpClient = new HttpClient(cmdUri, sessionPack.mSeqID, mHost);
			sessionPack.mHttpClient = httpClient;
			FlowControl.onSend(sessionPack.mCmdID, sessionPack.mCmdUri, (uint)sessionPack.mCacheBodyBuffer.Length);
			return httpClient.send(sessionPack.mCacheBodyBuffer, sessionPack.mCacheBodyBuffer.Length, null);
		}
		public static bool receiveList(List<Connector.RawPackBody> listPack)
		{
			SessionPackMgr.getPackWithHttpClient(delegate(SessionPack sessionPack)
			{
				int num = 0;
				byte[] array = null;
				string text = null;
				SocketError socketError = sessionPack.mHttpClient.receive(ref array, ref num, ref text);
				if (socketError == 11004)
				{
					return false;
				}
				if (socketError == -1)
				{
					sessionPack.mHttpClient.close();
					sessionPack.mHttpClient = null;
					return false;
				}
				if (array != null && array.Length >= 2)
				{
					if (190 == array[0])
					{
						Connector.RawPackBody rawPackBody = new Connector.RawPackBody();
						rawPackBody.body = array;
						rawPackBody.int_0 = sessionPack.mCmdID;
						rawPackBody.int_1 = num;
						listPack.Add(rawPackBody);
						ShortConnector.mFailedCount = 0;
						sessionPack.mHttpClient.close();
						sessionPack.mHttpClient = null;
						HostService.mShortConnHosts.onCurrentHostSuccess();
						return true;
					}
				}
				Log.smethod_1("Network", "received invalid reponse body, seq = " + num);
				sessionPack.mHttpClient.close();
				sessionPack.mHttpClient = null;
				return false;
			});
			return true;
		}
	}
}
